#pragma execution_character_set("utf-8")
#include "LoginRegisterWindow.h"
LoginRegisterWindow::LoginRegisterWindow(QWidget* parent)
        : QWidget(parent) {
    setWindowTitle("登录 / 注册");
    resize(350, 300);

    stackedWidget = new QStackedWidget(this);
    setupLoginPage();
    setupRegisterPage();

    stackedWidget->addWidget(loginPage);
    stackedWidget->addWidget(registerPage);
    stackedWidget->setCurrentIndex(0);

    switchBtn = new QPushButton("没有账号？点击注册");

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(stackedWidget);
    mainLayout->addWidget(switchBtn);

    setLayout(mainLayout);

    connect(switchBtn, &QPushButton::clicked, this, &LoginRegisterWindow::switchPage);
}

void LoginRegisterWindow::setupLoginPage() {
    loginPage = new QWidget();

    QLabel* userLabel = new QLabel("用户名：");
    QLabel* passLabel = new QLabel("密码：");

    loginUserEdit = new QLineEdit();
    loginPassEdit = new QLineEdit();
    loginPassEdit->setEchoMode(QLineEdit::Password);

    QPushButton* loginBtn = new QPushButton("登录");

    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(userLabel);
    layout->addWidget(loginUserEdit);
    layout->addWidget(passLabel);
    layout->addWidget(loginPassEdit);
    layout->addWidget(loginBtn);

    loginPage->setLayout(layout);

    connect(loginBtn, &QPushButton::clicked, this, &LoginRegisterWindow::login);
}

void LoginRegisterWindow::setupRegisterPage() {
    registerPage = new QWidget();

    QLabel* userLabel = new QLabel("用户名：");
    QLabel* passLabel = new QLabel("密码：");
    QLabel* confirmLabel = new QLabel("确认密码：");

    regUserEdit = new QLineEdit();
    regPassEdit = new QLineEdit();
    regConfirmEdit = new QLineEdit();
    regPassEdit->setEchoMode(QLineEdit::Password);
    regConfirmEdit->setEchoMode(QLineEdit::Password);

    QPushButton* regBtn = new QPushButton("注册");

    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(userLabel);
    layout->addWidget(regUserEdit);
    layout->addWidget(passLabel);
    layout->addWidget(regPassEdit);
    layout->addWidget(confirmLabel);
    layout->addWidget(regConfirmEdit);
    layout->addWidget(regBtn);

    registerPage->setLayout(layout);

    connect(regBtn, &QPushButton::clicked, this, &LoginRegisterWindow::registerUser);
}

void LoginRegisterWindow::switchPage() {
    isLogin = !isLogin;
    if (isLogin) {
        stackedWidget->setCurrentWidget(loginPage);
        switchBtn->setText("没有账号？点击注册");
    }
    else {
        stackedWidget->setCurrentWidget(registerPage);
        switchBtn->setText("已有账号？点击登录");
    }
}

void LoginRegisterWindow::login() {
    QString user = loginUserEdit->text();
    QString pass = loginPassEdit->text();

    if (user == "admin" && pass == "123456") {
        QMessageBox::information(this, "登录成功", "欢迎回来！");
    }
    else {
        QMessageBox::warning(this, "登录失败", "用户名或密码错误！");
    }
}

void LoginRegisterWindow::registerUser() {
    QString user = regUserEdit->text();
    QString pass = regPassEdit->text();
    QString confirm = regConfirmEdit->text();

    if (user.isEmpty() || pass.isEmpty() || confirm.isEmpty()) {
        QMessageBox::warning(this, "注册失败", "请填写所有字段！");
        return;
    }

    if (pass != confirm) {
        QMessageBox::warning(this, "注册失败", "两次密码输入不一致！");
        return;
    }

    QMessageBox::information(this, "注册成功", "请返回登录界面登录！");
    switchPage();
}
